package cn.yunhe.spring.struts.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import cn.yunhe.spring.mybatis.model.Page;
import cn.yunhe.spring.mybatis.model.Result;
import cn.yunhe.spring.mybatis.model.User;
import cn.yunhe.spring.mybatis.service.IUserService;

/**
 * 用户控制器
 * 
 * @author ZSL
 *
 */
@RestController
@RequestMapping("/user")
public class UserController{
	
	/**
	 * 注入用户业务服务
	 */
	@Autowired
	private IUserService userService;

	
	/**
	 * 查询单个用户详细信息
	 * 
	 * @return
	 */
	@RequestMapping("/editpage")
	public ModelAndView findUser(User user){
		User rsUser = userService.getUser(user);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("data", rsUser);
		return new ModelAndView("user_edit",rsMap);
	}
	
	/**
	 * 增加新用户
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	public Result addUser(User user){
		userService.addUser(user);
		return new Result();
	}
	
	/**
	 * 删除用户
	 * 
	 * @return
	 */
	@RequestMapping("/del")
	public Result delUser(User user){
		userService.delUser(user);
		return new Result();
	}
	
	/**
	 * 编辑用户
	 * 
	 * @return
	 */
	@RequestMapping("/edit")
	public Result editUser(User user){
		userService.editUser(user);
		return new Result();
	}
	
	/**
	 * 模糊查询用户列表
	 * 
	 * @return
	 */
	@RequestMapping("/getcond")
	public Page queryLikeUsers(int pageSize,int pageNum,User user){
		Map<String,Object> cond=new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("user", user);
		Page page = userService.queryLikeUsers(cond);
		return page;
	}


	
}
